package copy;

import java.io.*;

/**
 * 利用BufferedReader和BufferedWriter拷贝10.txt文件
 */

public class CopyDemo02 {
    public static void main(String[] args)throws Exception {
        //method01();
        //1.利用BufferedReader和BufferedWriter关联源文件和目的文件
        BufferedReader br = new BufferedReader(new FileReader("day23-IO-02/10.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("day23-IO-02/copy02_10.txt"));

        //2.读一行写一行
        String line;
        while ((line = br.readLine()) != null) {//readLine()读取每行字符(不包括换行符)
            bw.write(line);
            bw.newLine();//在每行写入一个换行符,保持跟原数据一致
        }

        //3.关流释放资源
        br.close();
        bw.close();
    }

    private static void method01() throws IOException {
        //10.txt =======> copy_10.txt
        //1.利用BufferedReader和BufferedWriter关联源文件和目的文件
        BufferedReader br = new BufferedReader(new FileReader("day23-IO-02/10.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("day23-IO-02/copy_10.txt"));

        //2.利用循环读写操作
        int ch;
        while ((ch = br.read()) != -1) {//read()方法无论文本是什么内容都会读取(包含换行符\r\n)
            bw.write(ch);
        }

        //3.关流释放资源
        br.close();
        bw.close();
    }
}
